﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using EventCallBack;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using UnityEngine.Networking;
using System;

namespace Yu
{
    public class Item : MonoBehaviour, IPointerDownHandler
    {
        private Vector2 TargetPoint;// 

        public float defultY;//原始的高度

        public TargetImage targetImage = null;
        float ItemWidth;
        float ItemHight;
        Image image;

        private Transform ItemChild;
        private int range;

        void Start()
        {
            image = GetComponent<Image>();
            ItemWidth = GetComponent<RectTransform>().sizeDelta.x;
            ItemHight = GetComponent<RectTransform>().sizeDelta.y;
            defultY = transform.localPosition.y;
            range = PlayerPrefs.GetInt("Range");
            ItemChild = transform.Find("ItemChild").transform;
            string URL = PictureCenter.Instance.pictureItems.GetPictureRandom;
            StartCoroutine(LoadPicture(URL,e=> 
            {
                Debug.Log(URL);
                ItemChild.GetComponent<Image>().sprite = Texture2DToSpiret(e);
            }));
        }

        /// <summary>
        /// 加载Texture2D
        /// </summary>
        /// <param name="Url"></param>
        /// <param name="texture"></param>
        /// <returns></returns>
        private static IEnumerator LoadPicture(string Url, Action<Texture2D> action)
        {
            if (Url!="")
            {
                using (WWW www = new WWW(Url))
                {
                    yield return www;
                    action(www.texture);
                }
            }
        }

        private Sprite Texture2DToSpiret(Texture2D texture2D)
        {
            return Sprite.Create(texture2D, new Rect(0, 0, texture2D.width, texture2D.height), Vector2.zero);
        }

        // Update is called once per frame
        void Update()
        {
            if (transform.localPosition.x < ItemGroup.Instance.Zero.x - ItemGroup.Instance.ItemSize.x - ItemGroup.Instance.itemsDir)
            {
                transform.localPosition = new Vector3(ItemGroup.Instance.LastPos, transform.localPosition.y, transform.localPosition.z);
            }

            if (transform.localPosition.x > ItemGroup.Instance.LastPos)
            {
                transform.localPosition = new Vector3(ItemGroup.Instance.FirstPos, transform.localPosition.y, transform.localPosition.z);
            }
            transform.Translate(Vector2.left * ItemGroup.Instance.ItemMoveSpeed * Time.deltaTime);
          
            foreach (var item in ItemGroup.targetPoint)
            {
                if (Vector2.Distance(transform.localPosition, item.transform.localPosition) < range)
                {
                    if (item.transform.localPosition.y > transform.localPosition.y)
                    {
                        transform.Translate(Vector2.down * 1000 * Time.deltaTime);
                    }
                    else
                    {
                        transform.Translate(Vector2.up * 1000 * Time.deltaTime);
                    }
                }
                if (Vector2.Distance(transform.localPosition, item.transform.localPosition) > range)
                {
                    BackPoint();
                }
            }
            if (ItemGroup.targetPoint.Count == 0)
            {
                BackPoint();
            }
        }

        public void BackPoint()
        {
            float step = 1000 * Time.deltaTime;
            transform.localPosition = Vector3.MoveTowards(transform.localPosition, new Vector3(transform.localPosition.x, defultY, 0), step);
        }

        public void OnPointerDown(PointerEventData eventData)
        {
            Debug.Log("点击到");
            //创建
            if (ItemGroup.targetPoint.Count > 0)
            {
                Destroy(ItemGroup.targetPoint[0].gameObject);
            }
            
            GameObject obj = Resources.Load<GameObject>("TargetImage");
            GameObject targetImage = Instantiate(obj, GameObject.Find("MainCanvas").transform);
            RectTransform rect = targetImage.GetComponent<RectTransform>();
            float MaxX = ItemGroup.Instance.ScreenSize.x/2 - rect.sizeDelta.x / 2;
            float MinX = ItemGroup.Instance.Zero.x + rect.sizeDelta.x / 2;

            float MaxY = ItemGroup.Instance.ScreenSize.y/2 - rect.sizeDelta.y/2 ;
            float MinY = ItemGroup.Instance.Zero.y + rect.sizeDelta.y / 2;

            Debug.Log(MaxX + ":" + MaxY);
            Debug.Log(MinX + ":" + MinY);
            float tempX = Mathf.Clamp(transform.localPosition.x, MinX, MaxX);
            float temyY = Mathf.Clamp(transform.localPosition.y, MinY, MaxY);

            targetImage.transform.localPosition = new Vector3(tempX, temyY, 0);
            targetImage.GetComponent<TargetImage>().SetImage(ItemChild.GetComponent<Image>().sprite);
        }
    }
}